package com.gxa.saas.portal.controller;

import com.alipay.api.AlipayApiException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.saas.core.exception.BusinessException;
import com.gxa.saas.core.exception.ExceptionCode;
import com.gxa.saas.core.vo.EditOderVo;
import com.gxa.saas.core.vo.TbAddressVo;
import com.gxa.saas.portal.service.AlipayService;
import com.gxa.saas.portal.service.GoodsIndentService;
import com.gxa.saas.portal.service.InsertIndentService;
//import com.gxa.saas.portal.thread.UserThreadLocal;
import com.gxa.saas.portal.thread.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resources;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Api(value = "c端下订单模块")
@Controller
@Validated
public class GoodsIndentController {

    @Autowired
    private GoodsIndentService goodsIndentService;
    @Autowired
    private InsertIndentService insertIndentService;
    @Autowired
    private AlipayService alipayService;

    @Autowired
    private UserThreadLocal userThreadLocal;

    @ApiOperation("确认订单选择地址")
    @ResponseBody
    @GetMapping("/GoodsIndent/placeOrder")
    public Map placeOrder() throws BusinessException, JsonProcessingException {
        String value = String.valueOf(userThreadLocal.get());
        ObjectMapper objectMapper = new ObjectMapper();
        com.gxa.saas.core.entity.vo.luoj.UserVO userVO = objectMapper.readValue(value, com.gxa.saas.core.entity.vo.luoj.UserVO.class);
        String userId = userVO.getId().toString();
        Map map = new HashMap();
        if (StringUtils.isEmpty(userId)){
            map.put("code",400);
            map.put("message","userId参数错误");
            return map;
        }
        map = this.goodsIndentService.selectPlaceOrder(userId);
        if (MapUtils.isEmpty(map)){
            map.put("code",400);
            map.put("message","无数据");
        }
        return map;
    }

    @ApiOperation("编辑订单选择商品信息")
    @ResponseBody
    @PostMapping("/GoodsIndent/editOder")
    public Map editOder(@RequestBody @Validated EditOderVo editOderVo) throws JsonProcessingException {
        String value = String.valueOf(userThreadLocal.get());
        ObjectMapper objectMapper = new ObjectMapper();
        com.gxa.saas.core.entity.vo.luoj.UserVO userVO = objectMapper.readValue(value, com.gxa.saas.core.entity.vo.luoj.UserVO.class);
        String userId = userVO.getId().toString();
        editOderVo.setUserId(userId);
        Map map = new HashMap();
        map =  this.goodsIndentService.editOder(editOderVo);
        if (MapUtils.isEmpty(map)){
            map.put("code",400);
            map.put("message","无数据");
        }
        return map;
    }

    @ApiOperation("支付订单接口")
    @ResponseBody
    @PostMapping("/GoodsIndent/IndentPayment")
    public void IndentPayment(HttpServletRequest httpRequest, HttpServletResponse httpResponse, @RequestBody TbAddressVo tbAddressVo, @RequestParam("odd") String odd) throws BusinessException, ServletException, IOException, AlipayApiException {
           System.out.println(tbAddressVo);
           System.out.println(odd);

        String key = this.goodsIndentService.insertIndentRedis(tbAddressVo, odd);
        System.out.println(key+"这里是controller层的key");
        if(StringUtils.isEmpty(key)){
            throw new BusinessException(ExceptionCode.PAY_FAILURE);
        }
        /*执行支付*/
        String s = this.goodsIndentService.IndentPayment(httpRequest, httpResponse, tbAddressVo, odd);
        if (StringUtils.isEmpty(s)){
            return;
        }
        httpResponse.setContentType("text/html;charset=GBK");
        httpResponse.getWriter().write(s); //直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();

        //将订单信息插入数据库
        this.insertIndentService.insertIndentMQ(key);



        this.insertIndentService.insertIndentSeletPayKey(key);



    }

    @ResponseBody
    @RequestMapping("/payccess")
    public void payccess(){
        System.out.println("成功了，支付成功了");
    }
}
